<?php
namespace common\modules\rbac\backend\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ArrayDataProvider;
use common\modules\rbac\backend\models\BizRule;
use common\modules\rbac\backend\components\RouteRule;

/**
 * Description of BizRule
 *
 * @author Xuegui Yuan <yuanxuegui@163.com>
 */
class BizRuleSearch extends Model
{

    /**
     *
     * @var string name of the rule
     */
    public $name;

    public function rules()
    {
        return [
            [
                [
                    'name'
                ],
                'safe'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'name' => Yii::t('backend', 'Name')
        ];
    }

    /**
     * Search BizRule
     * 
     * @param array $params            
     * @return \yii\data\ActiveDataProvider|\yii\data\ArrayDataProvider
     */
    public function search($params)
    {
        /* @var \yii\rbac\Manager $authManager */
        $authManager = Yii::$app->authManager;
        $models = [];
        $included = ! ($this->load($params) && $this->validate() && trim($this->name) !== '');
        foreach ($authManager->getRules() as $name => $item) {
            if ($name != RouteRule::RULE_NAME && ($included || stripos($item->name, $this->name) !== false)) {
                $models[$name] = new BizRule($item);
            }
        }
        
        return new ArrayDataProvider([
            'allModels' => $models
        ]);
    }
}
